{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-27T02:02:17.417910Z",
     "start_time": "2025-10-27T02:02:17.414959Z"
    }
   },
   "source": "",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-27T02:02:17.434924Z",
     "start_time": "2025-10-27T02:02:17.430071Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "from tqdm.notebook import tqdm,trange\n",
    "\n"
   ],
   "id": "d3caf836730afc1f",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-27T02:04:42.472730Z",
     "start_time": "2025-10-27T02:04:36.029830Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "422758d093f03105",
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mKeyboardInterrupt\u001B[39m                         Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[10]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m dataset = \u001B[43mLyricsDataset\u001B[49m\u001B[43m(\u001B[49m\u001B[43mseq_len\u001B[49m\u001B[43m=\u001B[49m\u001B[43mseq_len\u001B[49m\u001B[43m)\u001B[49m\n\u001B[32m      2\u001B[39m device = torch.device(\u001B[33m'\u001B[39m\u001B[33mcuda\u001B[39m\u001B[33m'\u001B[39m \u001B[38;5;28;01mif\u001B[39;00m torch.cuda.is_available() \u001B[38;5;28;01melse\u001B[39;00m \u001B[33m'\u001B[39m\u001B[33mcpu\u001B[39m\u001B[33m'\u001B[39m)\n\u001B[32m      3\u001B[39m model = TransformerGenerator(vocab_size=\u001B[38;5;28mlen\u001B[39m(dataset.word2index))\n",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[7]\u001B[39m\u001B[32m, line 25\u001B[39m, in \u001B[36mLyricsDataset.__init__\u001B[39m\u001B[34m(self, seq_len, file)\u001B[39m\n\u001B[32m     23\u001B[39m             \u001B[38;5;28mself\u001B[39m.word2index[word] = num_words\n\u001B[32m     24\u001B[39m             num_words += \u001B[32m1\u001B[39m\n\u001B[32m---> \u001B[39m\u001B[32m25\u001B[39m         indices.append(\u001B[38;5;28mself\u001B[39m.word2index[word])\n\u001B[32m     26\u001B[39m     indices.append(EOS)\n\u001B[32m     28\u001B[39m \u001B[38;5;28mself\u001B[39m.index2word = {v: k \u001B[38;5;28;01mfor\u001B[39;00m k, v \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m.word2index.items()}\n",
      "\u001B[31mKeyboardInterrupt\u001B[39m: "
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "def generate(model, start_tokens, max_len=20):\n",
    "    model.eval()\n",
    "    x = torch.tensor([start_tokens])\n",
    "    for _ in range(max_len):\n",
    "        with torch.no_grad():\n",
    "            logits = model(x)\n",
    "            next_token = logits[0, -1].argmax().item()\n",
    "            x = torch.cat([x, torch.tensor([[next_token]])], dim=1)\n",
    "            if next_token == dataset.word2index[\"<EOS>\"]:\n",
    "                break\n",
    "    return x[0].tolist()\n"
   ],
   "id": "dea937e97b88aced"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
